package cn.offcn.service.impl;

import cn.offcn.entity.*;
import cn.offcn.mapper.BlogMapper;
import cn.offcn.mapper.EvaluateMapper;
import cn.offcn.mapper.RoleMapper;
import cn.offcn.mapper.UserMapper;
import cn.offcn.service.UserService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.PageView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageView<User> getUsers(int currentPage,int pageSize,String uname) {

        //1.设置分页数据
        PageHelper.startPage(currentPage,pageSize);
        //2.查询
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(StringUtils.isNotBlank(uname)){
            criteria.andUnameLike("%"+uname+"%");
        }
        List<User> userList = userMapper.selectByExample(userExample);
        //3.创建PageInfo
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        //4.创建一个PageView对象
        PageView<User> pageView=new PageView<User>(currentPage,pageSize);
        pageView.setTotalrecords(pageInfo.getTotal());
        pageView.setRecords(pageInfo.getList());
        return pageView;
    }

    @Override
    public OAResult deleteUserById(int uid) {

        //先删除评论
        EvaluateExample evaluateExample=new EvaluateExample();
        EvaluateExample.Criteria evaluateExampleCriteria = evaluateExample.createCriteria();
        evaluateExampleCriteria.andUFkEqualTo(uid);
        evaluateMapper.deleteByExample(evaluateExample);
        //再删博客
        BlogExample blogExample=new BlogExample();
        BlogExample.Criteria blogExampleCriteria = blogExample.createCriteria();
        blogExampleCriteria.andUFkEqualTo(uid);
        blogMapper.deleteByExample(blogExample);
        //再删用户
        int rows= userMapper.deleteByPrimaryKey(uid);
        if(rows==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }

    @Override
    public User getUserById(int uid) {

        User user = userMapper.selectByPrimaryKey(uid);
        Role role=roleMapper.selectByPrimaryKey(user.getUtype());
        user.setRole(role);
        return user;
    }


    public OAResult getUserByUname(String username,String password) {

        UserExample userExample=new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUnameEqualTo(username);
        List<User> userList= userMapper.selectByExample(userExample);
        if(userList!=null && userList.size()>0){
            if(userList.get(0).getUpass().equals(password)){
                return OAResult.build(200,"查询成功",userList.get(0));
            }
        }

        return OAResult.build(400,"用户名或密码错误");
    }
}
